package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atguigu.gmall.product.service.BaseTrademarkService;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 品牌表 前端控制器
 *
 * @author atguigu
 * @since 2023-02-22
 */
@Api(tags = "品牌表控制器")
@RestController
@RequestMapping("/admin/product")
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    @Autowired
    private BaseCategoryTrademarkService baseCategoryTrademarkService;


    /**
     * 品牌分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result getBaseTrademarkByPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        //封装MP分页对象-封装页码 页大小
        IPage<BaseTrademark> iPage = new Page<>(page, limit);
        //调用业务逻辑进行分页查询封装其他数据
        iPage = baseTrademarkService.getBaseTrademarkByPage(iPage);
        return Result.ok(iPage);
    }

    /**
     * 品牌保存
     *
     * @param baseTrademark
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }


    /**
     * 根据品牌ID查询品牌信息
     *
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademarkById(@PathVariable("id") Long id) {
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }

    /**
     * 更新品牌
     *
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除品牌
     *
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result deleteBaseTrademarkById(@PathVariable("id") Long id) {
        baseTrademarkService.removeById(id);
        return Result.ok();
    }


    /**
     * 根据category3Id获取品牌列表
     *
     * @param category3Id
     * @return
     */
    @GetMapping("/baseCategoryTrademark/findTrademarkList/{category3Id}")
    public Result getTrademarkListByCategoryId(@PathVariable("category3Id") Long category3Id) {
        List<BaseTrademark> list = baseTrademarkService.getTrademarkListByCategoryId(category3Id);
        return Result.ok(list);
    }

    /**
     * 根据分类ID查询该分类未关联品牌
     *
     * @param category3Id 三级分类ID
     * @return
     */
    @GetMapping("/baseCategoryTrademark/findCurrentTrademarkList/{category3Id}")
    public Result getCurrentTrademarkList(@PathVariable("category3Id") Long category3Id) {
        List<BaseTrademark> list = baseTrademarkService.getCurrentTrademarkList(category3Id);
        return Result.ok(list);
    }


    /**
     * 将品牌关联到分类
     *
     * @param vo
     * @return
     */
    @PostMapping("/baseCategoryTrademark/save")
    public Result saveCategoryTrademark(@RequestBody CategoryTrademarkVo vo) {
        baseCategoryTrademarkService.saveCategoryTrademark(vo);
        return Result.ok();
    }


    /**
     * 解除品牌分类关联
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @DeleteMapping("/baseCategoryTrademark/remove/{category3Id}/{trademarkId}")
    public Result deleteCategoryTrademark(@PathVariable("category3Id") Long category3Id, @PathVariable("trademarkId") Long trademarkId){
        baseCategoryTrademarkService.deleteCategoryTrademark(category3Id, trademarkId);
        return Result.ok();
    }
}
